public class MySingleLinkedList {
    class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;

    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
    }

    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }


    //计算链表长度
    public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }


    //头插
    public void addFirst(int val) {
        ListNode node = new ListNode(val);
        node.next = head;
        head = node;
    }


    //尾插
    public void addLast(int val) {
        ListNode node = new ListNode(val);
        if (head == null) {
            head = node;
            return;
        }
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }


    //在任意位置插入
    public void addIndex(int index, int val) {
        //1. 判断index的合法性
        try {
            checkIndex(index);
        } catch (IndexNotLegalException e) {
            e.printStackTrace();
        }

        //2. index == 0 || index == size()
        if (index == 0) {
            addFirst(val);
            return;
        } else if (index == this.size()) {
            addLast(val);
            return;
        }

        //3. 找到 index 的前一个位置
        ListNode cur = findIndexSubOne(index);

        //4. 进行连接
        ListNode node = new ListNode(val);
        node.next = cur.next;
        cur.next = node;
    }


    public void checkIndex(int index) {
        if (index < 0 || index > size()) {
            throw new IndexNotLegalException("index位置不合法");
        }
    }

    private ListNode findIndexSubOne(int index) {
        ListNode cur = head;
        for (int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }
        return cur;
    }


    //判断链表中是否包含某个元素
    public boolean contains(int val) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == val) {
                return true;
            }
        }
        return false;
    }


    //删除第一次出现的关键字的节点
    public void remove(int val) {
        //链表为空
        if (head == null) {
            return;
        }
        //当第一个元素就为 val
        if (head.val == val) {
            head = head.next;
            return;
        }

        ListNode cur = head;
        while (cur.next != null) {
            if (cur.next.val == val) {
                ListNode del = cur.next;
                cur.next = del.next;
            }
            cur = cur.next;
        }
    }


    //删除所有出现的关键字节点
    public void removeAll(int val) {
        //1. 判空
        if (head == null) {
            return;
        }

        //2. 定义 prev 和 cur
        ListNode prev = head;
        ListNode cur = head.next;

        //3. 开始判断并删除
        while (cur != null) {
            if (cur.val == val) {
                prev.next = cur.next;
            } else {
                prev = cur;
            }
            cur = cur.next;
        }
        //4. 处理头结点
        if (head.val == val) {
            head = head.next;
        }
    }

    //清空链表
    public void clear() {
        head = null;
    }


}